﻿using System;
using System.Net;

namespace KKSGameLibrary.Logic
{
    public class OpenEdge
    {
        public int Row { set; get; }
        public int Column { set; get; }
        public CardType EdgeCardType { set; get; }
        public OpenDirection OpenDirection { set; get; }

        public OpenEdge GetMatchedEdge()
        {
            OpenDirection matchdir = OpenDirection.Top;
            int matchrow = 0;
            int matchcol = 0;
            switch (this.OpenDirection)
            {
                case OpenDirection.Left:
                    matchdir = OpenDirection.Right;
                    matchrow = this.Row;
                    matchcol = this.Column - 1;
                    break;
                case OpenDirection.Top:
                    matchdir = OpenDirection.Bottom;
                    matchrow = this.Row - 1;
                    matchcol = this.Column;
                    break;
                case OpenDirection.Right:
                    matchdir = OpenDirection.Left;
                    matchrow = this.Row;
                    matchcol = this.Column + 1;
                    break;
                case OpenDirection.Bottom:
                    matchdir = OpenDirection.Top;
                    matchrow = this.Row + 1;
                    matchcol = this.Column;
                    break;
            }

            return new OpenEdge
            {
                Row = matchrow,
                Column = matchcol,
                OpenDirection = matchdir,
                EdgeCardType = this.EdgeCardType
            };
        }

        public bool IsConnectable(OpenEdge bedge)
        {
            var matchededge = GetMatchedEdge();

            return matchededge.IsEquals(bedge);

            //if (this.Row == bedge.Row)
            //{
            //    //ab
            //    if (this.Column == bedge.Column - 1)
            //    {
            //        adir = KaKaSongGame.OpenDirection.Right;
            //        bdir = KaKaSongGame.OpenDirection.Left;
            //    }
            //    else
            //        if (this.Column == bedge.Column + 1)
            //        {
            //            adir = KaKaSongGame.OpenDirection.Left;
            //            bdir = KaKaSongGame.OpenDirection.Right;
            //        }
            //        else
            //            return true;
            //}
            //else
            //    if (this.Column == bedge.Column)
            //    {
            //        //a
            //        //b
            //        if (this.Row == bedge.Row - 1)
            //        {
            //            adir = KaKaSongGame.OpenDirection.Bottom;
            //            bdir = KaKaSongGame.OpenDirection.Top;
            //        }
            //        else
            //            //b
            //            //a
            //            if (this.Row == bedge.Row + 1)
            //            {
            //                adir = KaKaSongGame.OpenDirection.Top;
            //                bdir = KaKaSongGame.OpenDirection.Bottom;
            //            }
            //            else
            //                return true;
            //    }
            //    else
            //        return true;
        }

        public bool IsEquals(object obj)
        {
            var bedge = obj as OpenEdge;
            if (bedge != null &&
                this.Row == bedge.Row &&
                this.Column == bedge.Column &&
                this.OpenDirection == bedge.OpenDirection &&
                this.EdgeCardType == bedge.EdgeCardType)
                return true;
            else
                return false;
        }
    }

    public enum OpenDirection
    {
        Center,
        Left,
        Top,
        Right,
        Bottom
    }
}
